package com.youpai.platform.biggod.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.youpai.platform.biggod.service.convert.BigGodConverter;
import com.youpai.platform.biggod.service.entity.po.BigGodPO;
import com.youpai.platform.biggod.service.entity.po.SkillPO;
import com.youpai.platform.biggod.service.mapper.BigGodMapper;
import com.youpai.platform.biggod.service.mapper.SkillMapper;
import com.youpai.platform.biggod.web.entity.dto.BigGodDto;
import com.youpai.platform.biggod.web.entity.req.BigGodFreezeReq;
import com.youpai.platform.biggod.web.entity.req.BigGodListReq;
import com.youpai.platform.biggod.web.entity.resp.BigGodResp;
import com.youpai.platform.common.enums.ProcessCodeEnum;
import com.youpai.platform.common.exceptions.BaseBizException;
import com.youpai.platform.common.util.StringUtils;
import com.youpai.platform.biggod.service.service.BigGodService;
import com.youpai.platform.biggod.web.entity.dto.BigGodListDto;
import com.youpai.platform.biggod.web.entity.req.BigGodReq;
import com.youpai.platform.user.service.entity.po.UserPO;
import com.youpai.platform.user.service.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @time: 2022/8/1 1:02 下午
 * @author: welles
 * @description: 大神service实现类
 */
@Slf4j
@Component
public class BigGodServiceImpl implements BigGodService {

    @Resource
    private BigGodMapper bigGodCategoryMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private SkillMapper skillMapper;

    @Override
    public List<BigGodListDto> getBigGodList(BigGodListReq req) {
        List<Long> uids = new ArrayList<>();
        if(StringUtils.isEmpty(req.getGodCategory())){
            String keyword = req.getKeyword();
            keyword = StringUtils.parseKeyword(keyword);
            List<BigGodPO> bigGodPOS = bigGodCategoryMapper.getByName(keyword, null, 0, 100);
            uids = bigGodPOS.stream().map(BigGodPO::getUid).collect(Collectors.toList());
        }else {
            List<SkillPO> skillPOS = skillMapper.getBySkill(null, req.getGodCategory(), 0, 100);
            uids = skillPOS.stream().map(SkillPO::getGodUid).collect(Collectors.toList());
        }
        if(CollectionUtils.isEmpty(uids)){
            return Collections.emptyList();
        }else {
            List<UserPO> userPOS = userMapper.getUserFollowByUid(uids);
            return BigGodConverter.convertUserPOToBigGodListDto(userPOS);
        }
    }

    @Override
    public BigGodDto getGodSkills(BigGodReq req) {
        Long uid = Long.valueOf(req.getUid());
        //根据uid获取大神用户
        LambdaQueryWrapper<UserPO> userFollowPOQueryWrapper = new LambdaQueryWrapper<>();
        userFollowPOQueryWrapper
                .eq(UserPO::getUid, uid);
        UserPO userPO = userMapper.selectOne(userFollowPOQueryWrapper);
        //判断用户是否存在
        //根据用户ID获取技能列表
        List<SkillPO> skillPOS = skillMapper.getBySkill(uid, null, 0, 100);
        return BigGodConverter.convertUserPOAndSkillPOToBigGodDto(userPO, skillPOS);
    }

    @Override
    public BigGodResp freezeGod(BigGodFreezeReq req) {
        boolean flag = bigGodCategoryMapper.freezeGod(
                Long.valueOf(req.getUid()), Integer.valueOf(req.getFreeze())) > 0;
        if(flag){
            return new BigGodResp().setHintText("修改大神状态成功!");
        }else {
            log.error("修改大神状态失败");
            throw new BaseBizException(ProcessCodeEnum.CATEGORY_ADD_EXCEPTION);
        }
    }
}
